using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Demina;
using System;
using Microsoft.Xna.Framework.Input;

namespace DeminaRuntimeTest
{
	public class Game1 : Microsoft.Xna.Framework.Game
	{
		GraphicsDeviceManager graphics;
		SpriteBatch spriteBatch;

		List<string> animations = new List<string>();
		List<AnimationPlayer> animationPlayers = new List<AnimationPlayer>();
		List<Vector2> positions = new List<Vector2>();
		List<float> rotations = new List<float>();
		List<Vector2> scales = new List<Vector2>();
		List<bool> hflips = new List<bool>();
		List<bool> vflips = new List<bool>();
		List<Color> colors = new List<Color>();

		Random r = new Random();

		const int ANIM_COUNT = 100;

		public Game1()
		{
			graphics = new GraphicsDeviceManager(this);
			graphics.PreferredBackBufferWidth = 1280;
			graphics.PreferredBackBufferHeight = 720;
			Content.RootDirectory = "Content";
		}

		protected override void Initialize()
		{
			base.Initialize();
		}

		protected override void LoadContent()
		{
			spriteBatch = new SpriteBatch(GraphicsDevice);

			for (int i = 0; i < 6; i++)
			{
				animations.Add(i.ToString());
			}

			for (int i = 0; i < ANIM_COUNT; i++)
			{
				positions.Add(new Vector2((float)r.NextDouble() * 1280, (float)r.NextDouble() * 720));
				rotations.Add((float)r.NextDouble() * 6.28f);
				scales.Add(new Vector2((float)(r.NextDouble() * 2 + 0.0001), (float)(r.NextDouble() * 2 + 0.0001)));
				colors.Add(new Color((float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble(), (float)(r.NextDouble() + 1) / 2));
				hflips.Add(r.Next() % 2 == 0);
				vflips.Add(r.Next() % 2 == 0);

				AnimationPlayer player = new AnimationPlayer();
				foreach (string s in animations)
				{
					player.AddAnimation(s, Content.Load<Animation>("Animations/" + s));
				}
				player.StartAnimation(animations[r.Next(0, 6)]);
				animationPlayers.Add(player);
			}
		}

		protected override void UnloadContent()
		{
		}

		protected override void Update(GameTime gameTime)
		{
			if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Back) ||
				GamePad.GetState(PlayerIndex.Two).IsButtonDown(Buttons.Back) ||
				GamePad.GetState(PlayerIndex.Three).IsButtonDown(Buttons.Back) ||
				GamePad.GetState(PlayerIndex.Four).IsButtonDown(Buttons.Back) ||
				Keyboard.GetState().IsKeyDown(Keys.Escape))
			{
				Exit();
			}

			for (int i = 0; i < ANIM_COUNT; i++)
			{
				if (animationPlayers[i].Update(gameTime))
					animationPlayers[i].StartAnimation(animations[r.Next(0, 6)]);
			}

			base.Update(gameTime);
		}

		float asdf = 0;

		protected override void Draw(GameTime gameTime)
		{
			GraphicsDevice.Clear(Color.CornflowerBlue);

			for (int i = 0; i < ANIM_COUNT; i++)
			{
			    animationPlayers[i].Draw(spriteBatch, positions[i], hflips[i], vflips[i], rotations[i], colors[i], scales[i], Matrix.Identity);
			}

			asdf += 0.01f;
			base.Draw(gameTime);
		}

		static void Main(string[] args)
		{
			using (Game1 game = new Game1())
			{
				game.Run();
			}
		}
	}
}
